package com.chinawlx.wlxteacher.ui.adapter.calendar;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.bean.WLXCalendarBean;
import com.chinawlx.wlxteacher.ui.adapter.interf.OnItemClickListener;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.wlx_schedule;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者:吴名
 * 时间:16/9/7
 * 描述:课程表-某天课程列表
 */
public class WLXClassListAdapter extends RecyclerView.Adapter<WLXClassListAdapter.ItemViewHolder> {
    private ArrayList<wlx_schedule> mSchedules;
    private final Context mContext;
    private final LayoutInflater mLayoutInflater;
    private Calendar mCalendar;


    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public WLXClassListAdapter(Context context, ArrayList<wlx_schedule> schedules) {
        this.mContext = context;
        this.mSchedules = schedules;
        mLayoutInflater = LayoutInflater.from(mContext);
        mCalendar = Calendar.getInstance();
    }


    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(mLayoutInflater.inflate(R.layout.item_calendar_class, parent,
                false));
    }


    @Override
    public void onBindViewHolder(final ItemViewHolder holder, final int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(v, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(v, pos);
                    return true;
                }
            });
        }

        final wlx_schedule schedule = mSchedules.get(position);
        String classTypeCode = schedule.getClass_type_code();
        switch (classTypeCode) {
            case WLXConstant.kungfu:
                holder.mIvClassType.setImageResource(R.drawable.main_bg_level_kungfu);
                break;
            case WLXConstant.art:
                holder.mIvClassType.setImageResource(R.drawable.main_bg_level_art);
                break;
            case WLXConstant.dance:
                holder.mIvClassType.setImageResource(R.drawable.main_bg_level_dance);
                break;
            default:
                holder.mIvClassType.setImageResource(R.drawable.main_bg_level_music);
                break;
        }

        holder.mTvGradeName.setText(schedule.getGrade_title());
        //处理时间戳成时分格式
        mCalendar.setTimeInMillis(schedule.getOpening_date() * 1000);
        final String openDate = mCalendar.get(Calendar.HOUR_OF_DAY) + ":" + mCalendar.get
                (Calendar.MINUTE);
        mCalendar.setTimeInMillis(schedule.getClosing_date() * 1000);
        final String closeDate = mCalendar.get(Calendar.HOUR_OF_DAY) + ":" + mCalendar.get
                (Calendar.MINUTE);

        holder.mTvClassTime.setText(String.format("%s-%s", openDate, closeDate));

        // TODO: 16/9/8 服务器返回的是空数组,这个表还没有存
//        final List<wlx_student_study> studentStudies = WLXGreenDaoUtil.getStudentStudyDao()
//                .queryBuilder().where(wlx_student_studyDao.Properties.Student_id.eq(mStudentId),
//                        wlx_student_studyDao.Properties.Grade_id.eq(gradeId)).list();//查出来的值是唯一的

//        List<wlx_student_study> studentStudies = WLXGreenDaoUtil.getStudentStudyDao()
//                .queryBuilder().list();
//        WLXLogUtil.printE("studentStudies条数:" + studentStudies.size());
//        if (studentStudies.size() > 0) {
//            holder.mIvSate.setVisibility(View.VISIBLE);
//            final String studyStatusCode = studentStudies.get(0).getStudy_status_code();
//            switch (studyStatusCode) {
//                case "truant":
//                    holder.mIvSate.setImageResource(R.drawable.absenteeism);
//                    break;
//                case "leave":
//                    holder.mIvSate.setImageResource(R.drawable.askforleave);
//                    break;
//                case "stop":
//                    holder.mIvSate.setImageResource(R.drawable.suspendclasses);
//                    break;
//                default:
//                    holder.mIvSate.setVisibility(View.GONE);
//                    break;
//            }
//        } else {
//            holder.mIvSate.setVisibility(View.GONE);
//        }
        //判断课程四种状态
        String extendInfo = mSchedules.get(position).getExtend_info();
        if (!extendInfo.equals("null")) {
            WLXCalendarBean.DataBean.ExtendBean extendBean = new Gson().fromJson(extendInfo,
                    WLXCalendarBean.DataBean.ExtendBean.class);
            String teacher_change_code = extendBean.getTeacher_change_code();
            if (!TextUtils.isEmpty(teacher_change_code)) {
                holder.mIvSate.setVisibility(View.VISIBLE);
                switch (teacher_change_code) {
                    case "adjust":      //调课
                        holder.mIvSate.setImageResource(R.drawable.tiao);
                        break;
                    case "leave":       //请假
                        holder.mIvSate.setImageResource(R.drawable.qing);
                        break;
                    case "stop":        //停课
                        holder.mIvSate.setImageResource(R.drawable.ting);
                        break;
                    case "addition":    //加课
                        holder.mIvSate.setImageResource(R.drawable.jia);
                        break;
                    default:
                        holder.mIvSate.setVisibility(View.GONE);
                        break;
                }
            } else {
                holder.mIvSate.setVisibility(View.GONE);
            }
        }else {
            holder.mIvSate.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mSchedules == null ? 0 : mSchedules.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_classType)
        ImageView mIvClassType;
        @BindView(R.id.tv_gradeName)
        TextView mTvGradeName;
        @BindView(R.id.tv_classTime)
        TextView mTvClassTime;
        @BindView(R.id.iv_state)
        ImageView mIvSate;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
